package com.yuanda.wordhot.util.impl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;

import com.yuanda.wordhot.util.IPage;

public class HibernatePage implements IPage {

	private static final Log log = LogFactory.getLog(HibernatePage.class);

	protected List elements;

	protected int pageSize;

	protected int pageNumber;
	protected int totalElements = 0;

	private HibernatePage(int pageNumber, int pageSize) {
		this.pageNumber = pageNumber;
		this.pageSize = pageSize;
	}

	@Override
	public boolean isFirstPage() {
		return getPageNumber() == 0;
	}

	@Override
	public boolean isLastPage() {
		return getPageNumber() >= getLastPageNumber();
	}

	@Override
	public boolean hasNextPage() {
		return elements.size() > getPageSize();
	}

	@Override
	public boolean hasPreviousPage() {
		return getPageNumber() > 0;
	}

	@Override
	public int getLastPageNumber() {

		double totalResults = new Integer(getTotalNumberOfElements()).doubleValue();
		return new Double(totalResults / getPageSize() == Math.floor(totalResults / getPageSize()) ? Math
				.floor(totalResults / getPageSize()) - 1 : Math.floor(totalResults / getPageSize())).intValue();
	}

	@Override
	public List getThisPageElements() {
		/*
		 * Since we retrieved one more than the specified pageSize when the class
		 * was constructed, we now trim it down to the pageSize if a next page
		 * exists.
		 */
		return hasNextPage() ? elements.subList(0, getPageSize()) : elements;
	}

	@Override
	public int getTotalNumberOfElements() {
		return totalElements;
	}

	@Override
	public int getThisPageFirstElementNumber() {
		return getTotalNumberOfElements() > 0 ? getPageNumber() * getPageSize() + 1 : 0;
	}

	@Override
	public int getThisPageLastElementNumber() {
		int fullPage = getThisPageFirstElementNumber() + getPageSize() - 1;
		return getTotalNumberOfElements() < fullPage ? getTotalNumberOfElements() : fullPage;
	}

	@Override
	public int getNextPageNumber() {
		return getPageNumber() + 1;
	}

	@Override
	public int getPreviousPageNumber() {
		return getPageNumber() - 1;
	}

	@Override
	public int getPageSize() {
		return pageSize;
	}

	@Override
	public int getPageNumber() {
		return pageNumber;
	}

	public static HibernatePage getPageWithCount(Criteria criteria, int pageNumber, int pageSize) {
		HibernatePage sp = new HibernatePage(pageNumber, pageSize);
		Integer count = (Integer) criteria.setProjection(Projections.rowCount()).uniqueResult();
		criteria.setProjection(null);
		if (count != null) {
			sp.totalElements = count.intValue();
		} else {
			sp.totalElements = 0;
		}

		// 进行分页查询
		if (Integer.MAX_VALUE == sp.pageNumber) {
			sp.pageNumber = (sp.totalElements / sp.pageSize);
		}

		criteria.setFirstResult(sp.pageNumber * sp.pageSize);
		criteria.setMaxResults(sp.pageSize + 1);
		sp.elements = criteria.list();
		return sp;
	}
	

}
